define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            //重写添加和编辑框的大小
            $(".btn-add").data("area", ["50%", "89%"]);
            $(".btn-edit").data("area", ["50%", "90%"]);

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/shop/index' + location.search,
                    add_url: 'shop/shop/add',
                    edit_url: 'shop/shop/edit',
                    del_url: 'shop/shop/del',
                    multi_url: 'shop/shop/multi',
                    import_url: 'shop/shop/import',
                    table: 'shop',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                singleSelect: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('店铺名称'), operate: 'LIKE', align: 'left'},
                        {field: 'supervision.name', title: __('督导'), operate: 'LIKE'},
                        {field: 'shop.mobile', title: __('商家手机号'), operate: 'LIKE', visible: false},
                        {field: 'mobile', title: __('商家手机号'), operate: false},
                        {field: 'ratio', title: __('手续费'), operate: false},
                        {
                            field: 'shop.status',
                            title: __('店铺状态'),
                            // visible: false,
                            searchList: {"1": __('试营业'), "2": __('营业中'), "3": __('筹备中'), "4": __('休息中')},
                            formatter: function (value, row, index) {
                                var statusVal = row.status;
                                if (statusVal === '1') {
                                    return '<span style="background-color: #0e6f5c;color: #FFFFFF;padding: 3px 5px;border-radius: 3px">试营业</span>';
                                } else if (statusVal === '2') {
                                    return '<span style="background-color: #f39a0d;color: #FFFFFF;padding: 3px 5px;border-radius: 3px">营业中</span>';
                                } else if (statusVal === '3') {
                                    return '<span style="background-color: #f75444;color: #FFFFFF;padding: 3px 5px;border-radius: 3px">筹备中</span>';
                                } else if (statusVal === '4') {
                                    return '<span style="background-color: #1688f1;color: #FFFFFF;padding: 3px 5px;border-radius: 3px">休息中</span>';
                                } else {
                                    return '<span style="background-color: #444444;color: #FFFFFF;padding: 3px 5px;border-radius: 3px">其他</span>';
                                }
                            }
                        },
                        {
                            field: 'shop_type',
                            title: __('店铺类型'),
                            searchList: {
                                "棋牌": __('棋牌'),
                                "台球": __('台球'),
                                "棋牌台球": __('棋牌台球'),
                                "台球棋牌": __('台球棋牌')
                            },
                            formatter: Table.api.formatter.flag
                        },
                        {
                            field: 'profit_type',
                            title: __('抽佣类型'),
                            searchList: {
                                "0": __('普通抽点'),
                                "1": __('0抽佣'),
                                "2": __('全平台'),
                            },
                            formatter: Table.api.formatter.flag
                        },
                        {field: 'open_date', title: __('营业时间'), operate: false,formatter: Table.api.formatter.datetime},
                        {field: 'mt2_update_time', title: __('美团有效期'), operate: false,formatter: Table.api.formatter.datetime},
                        {field: 'city', title: __('城市'), operate: 'LIKE'},
                        {
                            field: 'address', title: __('地址'), operate: false, align: 'left',
                            formatter: function (value, row, index, field) {
                                if (value == null) {
                                    return "";
                                }
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + row.address + "'>" + value + "</span>";
                            },
                            cellStyle: function (value, row, index, field) {
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width": "300px"
                                    }
                                };
                            }
                        },
                        {field: 'ref_name', title: __('代理商'), operate: false},
                        {field: 'ref_mobile', title: __('代理商手机号')},
                        {field: 'agent_ratio', title: __('代理商手续费'), operate: false},
                        {
                            field: 'is_join_balance',
                            title: __('开通平台余额'),
                            searchList: {"1": __('已开通'), "-1": __('未开通')},
                        },
                        {
                            field: 'logo',
                            title: __('Logo'),
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image,
                            operate: false
                        },
                        {field: 'tel', title: __('客服电话'), operate: 'LIKE'},
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,

                            buttons: [
                                {
                                    name: 'list',
                                    title: __('包厢管理'),
                                    text: '包厢管理',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "shop/shoproom/index?shop_id=" + row['id'];
                                    }
                                },

                                {
                                    name: 'list',
                                    title: __('订单列表'),
                                    text: '订单列表',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "order/shoporder/index?shop_id=" + row['id'];
                                    }
                                },

                                {
                                    name: 'list',
                                    title: __('充值管理'),
                                    text: '充值订单',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "shop/shopcharge/index?shop_id=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('充值配置'),
                                    text: '充值配置',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "shop/shopchargeconfig/index?shop_id=" + row['id'];
                                    }
                                },


                                {
                                    name: 'list',
                                    title: __('预定列表'),
                                    text: '预定列表',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "shop/shoproombook/index?shop_id=" + row['id'];
                                    }
                                },

                                {
                                    name: 'list',
                                    title: __('店铺员工'),
                                    text: '店铺员工',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "shop/shopstaff/index?shop_id=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('包厢套餐'),
                                    text: '包厢套餐',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "shop/shoproomset/index?shop_id=" + row['id'];
                                    }
                                },
                                {
                                    name: 'list',
                                    title: __('资金流水'),
                                    text: '资金流水',
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    url: function (row) {
                                        return "money/shoporderprofit/index?shop_id=" + row['id'];
                                    }
                                },


                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                $(table).data("operate-del", null);
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            table.on('post-body.bs.table', function () {
                $(".btn-editone").data("area", ["50%", "89%"]);
                $(".btn-addone").data("area", ["50%", "90%"]);
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('#check_mobile').on('click', function (event) {
                event.preventDefault();//阻止表单发起默认请求
                const inputMobile = $('#c-mobile').val();
                // console.log('点击获取用户操作值：' + inputMobile);
                if (inputMobile === "") {
                    alert('手机号码不能为空');
                } else {
                    layer.msg('加载中...', {icon: 16, shade: 0.01, time: 10 * 1000});
                    //console.log('获取输入框的值为：' + firstIput);
                    getUser(inputMobile);
                    layer.closeAll();
                }
            });

            //查询saas用户
            function getUser(mobile) {
                $.ajax({
                    type: "post",
                    url: "/admin/shop/shop/getUser",
                    async: true,
                    data: {'mobile': mobile},
                    success: function (res) {
                        if(res.code === 0){
                            alert(res.msg);
                        }else{
                            alert('用户uid为：【'+res.data+'】');
                        }
                    },
                    error: function (res) {
                        alert('查询失败');
                    }
                });
            };

            Controller.api.bindevent();
        },
        edit: function () {

            var selVal = $("input[name='row[profit_type]']:checked").val();
            if(selVal === '1'){
                $("#min_amount").css('display','');
                $("#single_amount").css('display','');
                $("#start_date").css('display', '');
            }

            $("input[name='row[profit_type]']").change(function() {
                var selectedValue = $(this).val();
                // console.log("Selected value: " + selectedValue);
                if(selectedValue === '1'){
                    $("#min_amount").css('display','');
                    $("#single_amount").css('display','');
                    $("#start_date").css('display', '');
                }else{
                    $("#c-single_amount").val('0');
                    $("#c-min_amount").val('0');
                    $("#c-start_date").val(' ');
                    $("#start_date").css('display','none');
                    $("#min_amount").css('display','none');
                    $("#single_amount").css('display','none');
                }
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
